package mall.kotlin.com.myapplication.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import mall.kotlin.com.myapplication.R;

/**
 * USER: zjw
 * DATA: 2018/11/14 0014
 * TIME: 上午 10:25
 */
public class RectView extends View {
    Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int mColor;

    public RectView(Context context) {
        super(context);
        intPaint();
    }

    public RectView(Context context,  AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RectView);
        mColor = typedArray.getColor(R.styleable.RectView_rect_root, Color.RED);
        typedArray.recycle();
        intPaint();
    }

    public RectView(Context context,  AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        intPaint();
    }

    private void intPaint() {
        mPaint.setColor(mColor);
        mPaint.setStrokeWidth(1.5f);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int wmode = MeasureSpec.getMode(widthMeasureSpec);
        int wsize = MeasureSpec.getSize(widthMeasureSpec);
        int hmode = MeasureSpec.getMode(heightMeasureSpec);
        int hsize = MeasureSpec.getSize(heightMeasureSpec);

        if (wmode == MeasureSpec.AT_MOST && hmode == MeasureSpec.AT_MOST){
            setMeasuredDimension(600,600);
        } else if(wmode == MeasureSpec.AT_MOST){
            setMeasuredDimension(600,hsize);
        } else if(hmode == MeasureSpec.AT_MOST){
            setMeasuredDimension(wsize,600);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();
        int width = getWidth() - paddingLeft - paddingRight;
        int height = getHeight() - paddingTop - paddingBottom;
        canvas.drawRect(0+paddingLeft,0+paddingTop,getWidth() - paddingRight,getHeight() - paddingBottom,mPaint);
    }
}
